<template>
	<view class="dashboard-container">
		<web-view :src="dashboardUrl"></web-view>
	</view>
</template>

<script>
	import { baseURL } from '@/utils/request.js'
	export default {
		data() {
			return {
				dashboardUrl: '',
				baseUrl: baseURL + '/dashboard/'
			}
		},
		onLoad(options) {
			if (options.id) {
				// 获取存储的 token 和 refreshToken
				const token = uni.getStorageSync('token')
				const refreshToken = uni.getStorageSync('refreshToken')
				
				if (!token || !refreshToken) {
					uni.showToast({
						title: '登录过期，请重新登录',
						icon: 'none'
					})
					// 跳转到登录页
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}, 1500)
					return
				}
				// 拼接完整的仪表盘URL，包含token和refreshToken
				this.dashboardUrl = `${this.baseUrl}${options.id}?accessToken=${encodeURIComponent(token)}&refreshToken=${encodeURIComponent(refreshToken)}`
			} else {
				uni.showToast({
					title: '缺少仪表盘ID',
					icon: 'none'
				})
				// 返回上一页
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
			}
		}
	}
</script>

<style>
.dashboard-container {
	width: 100%;
	height: 100vh;
}
</style>
